package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;


import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.FlexShelfOffOrderAdapter;
import com.mflex.WHSmartWhouseAd.model.ResTaskMfgOrderDto;
import com.mflex.WHSmartWhouseAd.model.TaskFlexOrder;
import com.mflex.WHSmartWhouseAd.utils.HideKeyBoardUtil;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xhttp2.reflect.TypeToken;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.net.JsonUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


public class FlexShelfOffOrderActivity extends AppCompatActivity {

    String userName;
    EditText stvTaskNo;
    EditText stvFloor;
    EditText stvSMTPN;
    EditText stvFlexPN;
    ListView txtList;

    String PlanQty;
    String ZoomScale;

    MiniLoadingDialog mMiniLoadingDialog;

    List<TaskFlexOrder> TaskFlexOrderlist = new ArrayList<>();

    private String TaskNo;
    private String SubTaskNo;
    private String Floor;
    private String SMTProduct;
    private String FLEXProduct;

    private String IsFeedingQty;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_flex_shelf_off_order);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        stvTaskNo= findViewById(R.id.stvTaskNo);
        stvFloor= findViewById(R.id.stvFloor);
        stvSMTPN= findViewById(R.id.stvSMTPN);
        stvFlexPN= findViewById(R.id.stvFlexPN);

        txtList= findViewById(R.id.lvList);


        Intent intent = getIntent();

        userName = intent.getStringExtra("userName");
        TaskNo = intent.getStringExtra("SubTaskNo");


//        stvFlexPN.setText(PlanQty);
        stvTaskNo.setText(TaskNo);
        stvFloor.setText(intent.getStringExtra("Floor"));
        stvSMTPN.setText(intent.getStringExtra("SMTProduct"));
        stvFlexPN.setText(intent.getStringExtra("FLEXProduct"));

        ZoomScale=intent.getStringExtra("ZoomScale");
        PlanQty=intent.getStringExtra("PlanQty").toString();

        initData();
        txtList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(FlexShelfOffOrderActivity.this, FlexShelfOffOrderDetailActivity.class);
                intent.putExtra("OrderQty", TaskFlexOrderlist.get(i).getWorkOrderQty());
                intent.putExtra("SMTLot", TaskFlexOrderlist.get(i).getMESLot());
                intent.putExtra("TaskNo", stvTaskNo.getText().toString());
                intent.putExtra("userName", userName);
                intent.putExtra("ZoomScale", ZoomScale);
                intent.putExtra("PlanQty", PlanQty);
                intent.putExtra("floor", stvFloor.getText().toString());
                intent.putExtra("SMTProduct", stvSMTPN.getText().toString());
                intent.putExtra("FLEXProduct", stvFlexPN.getText().toString());
                intent.putExtra("IsFeedingQty", IsFeedingQty);
//                startActivity(intent);
                startActivityForResult(intent,100);
            }
        });

    }

    private void initData() {
        mMiniLoadingDialog.show();
        XHttp.get("api/WHSmartWhousePDA/GetMfgOrderByTaskNo")
                .params("TaskNo", TaskNo)
                .syncRequest(false)
                .onMainThread(true)
                .timeOut(120000)
                .execute(new SimpleCallBack<ResTaskMfgOrderDto>() {
                    @Override
                    public void onSuccess(ResTaskMfgOrderDto response) {
                        mMiniLoadingDialog.dismiss();

                        IsFeedingQty = response.getIsFeedingQty();

                        TaskFlexOrderlist = response.getOrderList();
                        FlexShelfOffOrderAdapter ShipmentSapListAdapter = new FlexShelfOffOrderAdapter(FlexShelfOffOrderActivity.this, TaskFlexOrderlist);
                        txtList.setAdapter(ShipmentSapListAdapter);
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(FlexShelfOffOrderActivity.this, R.raw.error_voice);mp.start();
                        new MaterialDialog.Builder(FlexShelfOffOrderActivity.this)
                                .iconRes(R.drawable.icon_warning)
                                .title(R.string.tip_warning)
                                .content(Objects.requireNonNull(e.getMessage()))
                                .positiveText(R.string.lab_submit)
                                .show();
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        initData();
    }
}